package org.example.src.controller;

import org.example.src.dto.request.FinalReportRequest;
import org.example.src.dto.response.ApiResponse;
import org.example.src.dto.response.FinalReportResponse;
import org.example.src.entity.FinalReport;
import org.example.src.service.FinalReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/final-reports")
@CrossOrigin(origins = "*")
public class FinalReportController extends BaseController {

    @Autowired
    private FinalReportService finalReportService;

    @GetMapping
    @PreAuthorize("hasAnyRole('ADMIN', 'TEACHER')")
    public ResponseEntity<ApiResponse<List<FinalReport>>> getAllFinalReports() {
        List<FinalReport> finalReports = finalReportService.findAllFinalReports();
        return success(finalReports);
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'STUDENT', 'TEACHER', 'COMPANY')")
    public ResponseEntity<ApiResponse<FinalReport>> getFinalReportById(@PathVariable Long id) {
        FinalReport finalReport = finalReportService.findFinalReportById(id);
        if (finalReport == null) {
            return notFound("最终报告未找到");
        }
        return success(finalReport);
    }

    @PostMapping
    @PreAuthorize("hasAnyRole('ADMIN', 'STUDENT')")
    public ResponseEntity<ApiResponse<FinalReport>> createFinalReport(@Valid @RequestBody FinalReportRequest request) {
        FinalReport createdFinalReport = finalReportService.createFinalReport(request);
        return created(createdFinalReport);
    }

    @PutMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'STUDENT', 'TEACHER', 'COMPANY')")
    public ResponseEntity<ApiResponse<FinalReport>> updateFinalReport(@PathVariable Long id, @Valid @RequestBody FinalReportRequest request) {
        FinalReport updatedFinalReport = finalReportService.updateFinalReport(id, request);
        return success("最终报告更新成功", updatedFinalReport);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'STUDENT')")
    public ResponseEntity<ApiResponse<Void>> deleteFinalReport(@PathVariable Long id) {
        finalReportService.deleteFinalReport(id);
        return noContent();
    }

    @GetMapping("/student/{studentId}")
    @PreAuthorize("hasAnyRole('ADMIN', 'STUDENT', 'TEACHER') or (hasRole('STUDENT') and #studentId == authentication.principal.id)")
    public ResponseEntity<ApiResponse<List<FinalReport>>> getFinalReportsByStudentId(@PathVariable Long studentId) {
        List<FinalReport> finalReports = finalReportService.findFinalReportsByStudentId(studentId);
        return success(finalReports);
    }

    @GetMapping("/application/{applicationId}")
    @PreAuthorize("hasAnyRole('ADMIN', 'STUDENT', 'COMPANY', 'TEACHER')")
    public ResponseEntity<ApiResponse<List<FinalReport>>> getFinalReportsByApplicationId(@PathVariable Long applicationId) {
        List<FinalReport> finalReports = finalReportService.findFinalReportsByApplicationId(applicationId);
        return success(finalReports);
    }

    @GetMapping("/student/{studentId}/responses")
    @PreAuthorize("hasAnyRole('ADMIN', 'STUDENT', 'TEACHER') or (hasRole('STUDENT') and #studentId == authentication.principal.id)")
    public ResponseEntity<ApiResponse<List<FinalReportResponse>>> getFinalReportResponsesByStudentId(@PathVariable Long studentId) {
        List<FinalReportResponse> finalReports = finalReportService.getFinalReportsByStudentId(studentId);
        return success(finalReports);
    }
}
